var table;
var index;
layui.use('table', function () {
    table = layui.table;

    table.render({
        elem: '#list',
        id: "list",
        url: '../Media/getdata',
        toolbar: '#toolbarDemo',
        page: true,
        cols: [
            [{
                field: 'id',
                title: '编号',
                width: 100

            }, {
                field: 'media_name',
                title: '素材名称',
                width: 200

            }, {
                field: 'type',
                title: '文件类型',
                width: 100
            }, {
                field: 'media_id',
                title: '素材ID',
            }, {
                title: '操作',
                toolbar: '#barDemo',
                width: 80,
            }]
        ]

    });

    //编辑删除
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        // 查看弹出url页面
        if (obj.event === 'del') {
            layer.confirm('确认进行删除？', function (index) {
                $.get("../Media/del", { id: data.id }, function (ret) {
                    if (ret.code == 0) {
                        reload();
                    }
                    layui.layer.msg(ret.message);
                });

            });
        } else if (obj.event === 'edit') {
            $.get("../Media/edit", {}, function (html) {

                index = layui.layer.open({
                    id: "layedit",
                    title: "修改项目",
                    type: 1,
                    area: ['300px', '550px'],
                    content: html,
                    success: function (layero, index) {
                        $("#id").val(data.id);
                        $("#ask").val(data.ask);
                        $("#ret").val(data.ret);
                    },                        
                    btn: ['确定', '取消'],
                    btnAlign: 'c', //按钮居中
                    yes: function () {
                        $("#btnsave").click();
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });



            }, 'html');
        }

    });


    // 工具栏事件
    table.on('toolbar(list)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':

                $.get("../Media/add", {}, function (html) {
                    index = layui.layer.open({
                        id: "layadd",
                        title: "添加项目",
                        type: 1,
                        area: ['300px', '600px'],
                        content: html,
                        success: function (layero, index) {
                            // 
                        },
                        btn: ['确定', '取消'],
                        btnAlign: 'c', //按钮居中
                        yes: function () {
                            $("#btnsave").click();
                        },
                        btn2: function () {
                            layer.closeAll();
                        }
                    })
                }, 'html');

                break;
        };
    });

});
function reload() {
    table.reload('list', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , where: {
            key: {

            }
        }
    });
}